package org.sith.taskrunner.gwt.client.dataloader;

import com.google.gwt.user.client.rpc.AsyncCallback;

public class LoadDataAsyncCallback<T> implements AsyncCallback<T> {

    private ResultWatcher<T> resultWatcher;

    private ExecutionStateWatcher executionStateWatcher;

    private String loadDataDescription;
    private boolean hasNext = false;

    @Override
    public void onFailure(Throwable caught) {
        executionStateWatcher.executionState(loadDataDescription, false, false);
    }

    @Override
    public void onSuccess(T result) {
        if (result == null) {
            executionStateWatcher.executionState(loadDataDescription, false, false);
            return;
        }
        resultWatcher.dataLoaded(result);
        executionStateWatcher.executionState(loadDataDescription, true, hasNext);
    }

    public void setResultWatcher(ResultWatcher<T> resultWatcher) {
        this.resultWatcher = resultWatcher;
    }

    public void setExecutionStateWatcher(ExecutionStateWatcher executionStateWatcher) {
        this.executionStateWatcher = executionStateWatcher;
    }

    public void setLoadDataDescription(String loadDataDescription) {
        this.loadDataDescription = loadDataDescription;
    }

    public boolean isHasNext() {
        return hasNext;
    }

    public void setHasNext(boolean hasNext) {
        this.hasNext = hasNext;
    }
}
